@extends("backend.layout.main")

@section("content")
    <!-- Main content -->
    <section class="content">
        <!-- Small boxes (Stat box) -->
        <div class="row">
            <div class="col-lg-10 col-xs-6">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">文章列表</h3>
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body">
                        <table class="table table-bordered">
                            <tbody>
                            <tr>
                                <th style="width: 10px">ID</th>
                                <th>文章标题</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            @foreach($posts as $post)
                                <tr>
                                    <td>{{$post->id}}</td>
                                    <td>{{$post->title}}</td>
                                    <td class="status">{{$post->status}}</td>
                                    <td>
                                        <button type="button" class="btn  btn-block btn-default post-audit" data-id="{{$post->id}}"
                                                @if($post->status=="正常")style="display: none " @endif
                                                data-status="0" data-word="正常">正常
                                        </button>
                                        <button type="button" class="btn  btn-block btn-primary post-audit" data-id="{{$post->id}}"
                                                @if($post->status=="推荐")style="display: none " @endif
                                                data-status="1" data-word="推荐">推荐
                                        </button>
                                        <button type="button" class="btn  btn-block btn-danger post-audit" data-id="{{$post->id}}"
                                                @if($post->status=="禁止")style="display: none" @endif
                                                data-status="-1" data-word="禁止">拉黑
                                        </button>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                    {{$posts->links()}}
                </div>
            </div>
        </div>
    </section>
    <script>
        $('body').delegate('.post-audit', 'click', function () {
            word = $(this).data('word');
            id = $(this).data('id');
            status = $(this).data('status');
            th = $(this);

            $.ajax({
                url: "/backend/posts/" + id + "/status",
                type: "POST",
                dataType: 'json',
                headers: {
                    'X-CSRF-TOKEN': "{{csrf_token()}}"
                },
                data: {
                    'status': status,
                },
                success: function (data) {
                    if (data.code == 0) {
                        th.parent().parent().find('.status').html(word);
                        th.css('display','none').siblings().css('display','block');
                    }
                },
                error: function () {
                    swal('请求超时，稍后再试', '', 'error');
                }
            })
        })

    </script>
@endsection